﻿using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Regions;
using StockholmsStad.Windows.UI.Infrastructure;
using StockholmsStad.Windows.UI.Infrastructure.Events;
using StockholmsStad.Windows.UI.Modules.Maintenance.Controllers;
using StockholmsStad.Windows.UI.Modules.Maintenance.Interfaces;
using StockholmsStad.Windows.UI.Modules.Maintenance.Presenters;
using StockholmsStad.Windows.UI.Modules.Maintenance.Views;
using StockholmsStad.Windows.UI.Modules.Maintenance.Services;
using StockholmsStad.Windows.UI.Modules.Maintenance.Controls;

namespace StockholmsStad.Windows.UI.Modules.Maintenance
{
    public class MaintenanceModule : IModule
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;
        private readonly IEventAggregator eventAggregator;

        public MaintenanceModule(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.eventAggregator = eventAggregator;
        }

        protected void RegisterViewsAndServices()
        {
            container.RegisterType<IMaintenanceController, MaintenanceController>();
            container.RegisterType<IMaintenanceService, MaintenanceService>(new ContainerControlledLifetimeManager());
            container.RegisterType<IInformationTextService, InformationTextService>(new ContainerControlledLifetimeManager());
            container.RegisterType<IUserService, UserService>(new ContainerControlledLifetimeManager());
            container.RegisterType<ITempatesService, TemplatesService>(new ContainerControlledLifetimeManager());
            container.RegisterType<ICeremonySettingsService, CeremonySettingsService>(new ContainerControlledLifetimeManager());

            container.RegisterType<IUserView, TabItemUserView>();
            container.RegisterType<ITemplatesView, TabItemTemplateView>();
            container.RegisterType<ICeremonySettingsView, TabItemCeremonySettingsView>();
            container.RegisterType<IInformationTextView, TabItemInformationTextView>();
            container.RegisterType<IFilingView, TabItemFilingView>();
            container.RegisterType<IFilingListView, FilingListView>();
            container.RegisterType<IStatisticView, TabItemStatisticsView>();
            container.RegisterType<IMaintenanceView, MaintenanceView>();

            container.RegisterType<IScheduleView, ScheduleView>();
            container.RegisterType<IScheduleGenerateTimeView, ScheduleGenerateTimeView>();
        }

        public void LoadView()
        {
            var args = new UIEventArgs { ShellTitle = "Parvis - Underhåll" };
            eventAggregator.GetEvent<UIEvent>().Publish(args);

            var maintenancePresenter = container.Resolve<MaintenancePresenter>();
            var contentRegion = regionManager.Regions[RegionNames.ContentRegion];
            var loadedView = contentRegion.GetView(ViewNames.MaintenanceView);

            if (loadedView == null)
                contentRegion.Add(maintenancePresenter.View, ViewNames.MaintenanceView);
            
                contentRegion.Activate(maintenancePresenter.View);

            // Load Tabregions
            var userPresenter = container.Resolve<UserPresenter>();
            var templatePresenter = container.Resolve<TemplatesPresenter>();
            var ceremonyPresenter = container.Resolve<CeremonySettingsPresenter>();
            var textPresenter = container.Resolve<InformationTextPresenter>();
            var timesPresenter = container.Resolve<GenerateTimePresenter>();
            var filingPresenter = container.Resolve<FilingPresenter>();
            var statisticPresenter = container.Resolve<StatisticPresenter>();

            var tabRegion = regionManager.Regions["TabRegion"];

            tabRegion.Add(userPresenter.View, "User");
            tabRegion.Add(templatePresenter.View, "Template");
            tabRegion.Add(ceremonyPresenter.View, "1");
            tabRegion.Add(textPresenter.View, "2");
            tabRegion.Add(timesPresenter.View, "3");
            tabRegion.Add(filingPresenter.View, "5");
            tabRegion.Add(statisticPresenter.View, "6");
        }

        #region IModule Members

        public void Initialize()
        {
            RegisterViewsAndServices();
        }

        #endregion
    }
}